package com.rongshu.view.userInfo.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.userInfo.entity.SignDayResponse;
import com.rongshu.view.userInfo.entity.SignInBody;
import com.rongshu.view.userInfo.entity.SignListResponse;
import com.rongshu.view.userInfo.iview.ISignView;
import com.rongshu.widget.BanyanDialog;

import java.util.HashMap;

import rx.Subscriber;

/**
 * created by zhengweis on 2019/2/28.
 * description：
 */
public class SignPresenter extends BasePresenter<ISignView> {

    public SignPresenter(Context context) {
        super(context);
    }

    public void getSignMsg() {
        HashMap map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.getSelectSign(UserInfoUtils.getUserId(), Common.encryptionSign(map))
                .subscribe(new Subscriber<SignListResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().faild();
                    }

                    @Override
                    public void onNext(SignListResponse response) {
                        if (isViewAttached()) {
                            getView().bindView(response);
                        }
                    }
                });
    }

    public void getSignIn(final int remark, String date) {
        HashMap map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("remark", remark);
        SignInBody body = new SignInBody();
        body.setSignInDate(date);
        RetorfitService.postSignIn(UserInfoUtils.getUserId(), remark,
                Common.encryptionSign(map), body)
                .subscribe(new Subscriber<SignDayResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().faild();
                    }

                    @Override
                    public void onNext(SignDayResponse response) {
                        if (isViewAttached()) {
                            if (remark == 2) {
                                getView().suppleSignSuccess(response);
                            } else {
                                getView().signSuccess(response);
                            }
                        }
                    }
                });
    }

    public void showDialog(int fraction) {
        BanyanDialog.Builder builder = new BanyanDialog.Builder(mContext);
        builder.setTitle("恭喜你");
        builder.setContent("签到成功，获得 +" + fraction + " 积分");
        builder.setRight("我知道了");
        final BanyanDialog banyanDialog = builder.create();
        builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
            @Override
            public void leftClickListener() {

            }

            @Override
            public void rightClickListener() {
                banyanDialog.dismiss();
            }
        });
        banyanDialog.show();
    }
}
